
let sId = $("#sId").val();
let answer = new Map;

function getChapter() {
    let table = $("#table");
    $.ajax({
        type: "GET",
        url: "webapi/exercise/getAllChapter",
    }).done(function (data){
        for (const key in data) {
            let chapter = data[key];
            let chapterId = $("<td>" + chapter.id + "</td>");
            let chapterName = $("<td>" + chapter.name + "</td>");
            let completionRate = $("<td></td>");
            $.ajax({
                type: "GET",
                url: "webapi/exercise/getChapterCompletionRate",
                data: {
                    cId: chapter.id,
                    sId: sId
                }
            }).done(function (data){
                let rate = data * 100 + "%";
                completionRate.text(rate);
            });
            let button = $("<td><a class='button small' href='/toChapterExercise?cId=" + chapter.id + "'>开始答题</a></td>");

            let chapterInfo = $("<tr></tr>").append(chapterId).append(chapterName).append(completionRate).append(button);
            table.append(chapterInfo);
        }
    });
}

async function getExerciseSize(){
    let cId = $("#cId").val();
    $.ajax({
        type: "GET",
        url: "webapi/exercise/getExerciseSizeByCid",
        async: false,
        data: {
            cId: cId
        }
    }).done(function (data){
        $("#size").val(data);
    });
}

function getExercise(count){
    let cId = $("#cId").val();
    $.ajax({
        type: "GET",
        url: "webapi/exercise/getExerciseByCid",
        data: {
            cId: cId,
            count: count
        }
    }).done(function (map){
        if (map !== ""){
            for (const key in map) {
                let exercise = setExercise(key);
                let optionList = map[key];
                //console.log(exercise);
                //console.log(optionList);
                $("#eid").val(exercise.get('id'));
                $("#title").text(count + 1 + "." + exercise.get('title'));
                $("#optionA").siblings("label").text("A." + optionList.optionA);
                $("#optionB").siblings("label").text("B." + optionList.optionB);
                $("#optionC").siblings("label").text("C." + optionList.optionC);
                $("#optionD").siblings("label").text("D." + optionList.optionD);
            }

        } else {
            alert("没有下一道题了");
        }
    });
}

function switchRadio(option){

    let optionA = $("#optionA");
    let optionB = $("#optionB");
    let optionC = $("#optionC");
    let optionD = $("#optionD");

    let optionList = [];
    optionList.push(optionA);
    optionList.push(optionB);
    optionList.push(optionC);
    optionList.push(optionD);

    for (const key in optionList) {
        let value = optionList[key];
        if (value.is($(option))){
            value.prop("checked", !value.checked);
        } else {
            value.prop("checked", false);
        }
    }
}

function getAnwser() {

    let eid = $("#eid").val();

    let optionA = $("#optionA");
    let optionB = $("#optionB");
    let optionC = $("#optionC");
    let optionD = $("#optionD");

    let optionList = [];
    optionList.push(optionA);
    optionList.push(optionB);
    optionList.push(optionC);
    optionList.push(optionD);

    for (const key in optionList) {
        let value = optionList[key];
        if (value.prop("checked")){
            //console.log(value.val());
            answer.set(eid, value.val());
            value.prop("checked", false);
        }
    }
}

function submitAnswer() {
    let size = $("#size").val();

    let obj = new Object(null);
    for (let[k, v] of answer){
        obj[k] = v;
    }

    let sid = $("#sid").val();
    if (confirm("是否提交？")){
        $.ajax({
            type: "POST",
            url: "webapi/exercise/submitAnswer",
            dataType: "json",
            data: {
                answerMap: JSON.stringify(obj),
                sid: sid
            }
        }).done(function (data) {
            if (data >= 0){
                alert("提交成功, 一共答对了" + data + "道题目，加油！");
                window.location.href = "/toChapterList";
            } else {
                alert("提交失败");
            }
        })
    }
}